package com.aistar.service.user.impl;


import com.aistar.mapper.EmployeeMapper;
import com.aistar.mapper.ResumeMapper;
import com.aistar.mapper.UserMapper;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Employer;
import com.aistar.model.entity.Resume;
import com.aistar.model.entity.User;
import com.aistar.service.user.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Integer registerByEmail(User user, Resume resume, Employee employee) {
        int count=0;
        User user0=userMapper.findByEmail(user.getEmail());
        User user1=userMapper.findUserByUsername(user.getUsername());
        if (null==user0) {
            if(null==user1) {
                count = userMapper.insertByEmail(user);
                if (1 == count) {
                    count += resumeMapper.insertResume(resume);
                    if (2 == count) {
                        int userId = userMapper.findByEmail(user.getEmail()).getId();
                        employee.setUserId(userId);
                        count += employeeMapper.insertEmployee(employee);
                    }
                }
                int userId = userMapper.findByEmail(user.getEmail()).getId();
                if (1 == count) {
                    userMapper.deleteUser(userId);
                }
                if (2 == count) {
                    userMapper.deleteUser(userId);
                    employeeMapper.deleteByUserId(userId);
                }
            }else {
                count = -1;
            }
        }
        return count;
    }
//    @Override
//    public User judgeRegister(String username, String password) {
//        // 首先根据用户名获取用户实体
//        User user = userMapper.findUserByUsername(username);
//        // 判断是否能查询出用户
//        if (user == null)
//            return null;
//        // 到这表示是有用户的,然后需要进行密码校验
//        String codes = user.getPassword();
//        if (encoder.matches(password, codes)) {
//            // 验证成功,返回user
//            return user;
//        } else {
//            // 验证失败,返回空
//            return null;
//        }
//    }
//
//    @Override
//    public Integer sendEmailCoder(String email) {
//        // 首先判断该邮箱是否存在数据库中
//        User user = userMapper.findByEmail(email);
//        if (null == user)
//            return -1;
//
//        // 这一步表示该邮箱是存在在数据库中的,进行邮件发送功能
//        SimpleMailMessage msg = new SimpleMailMessage();
//        msg.setFrom("2294014200@qq.com");
//        msg.setSubject("轻聘-简单生活,轻松应聘");
//        // 获取验证码
//        int num = (int) (Math.random() * 90000 + 10000);
//        // 把验证码放到redis中并设置过期时间为五分钟
//        ValueOperations vop = redisTemplate.opsForValue();
//        vop.set(email, String.valueOf(num), 5, TimeUnit.MINUTES);
//
//        // 发送验证码
//        msg.setText("验证码:" + num + "\n请在五分钟内完成验证");
//        msg.setTo(email);
//        // 真正的发送
//        mailSender.send(msg);
//        return 1;
//    }
//
////    @Override
////    public Integer judgeLoginByEmailr(String email, String code) {
////        // 首先验证邮箱是否存在在数据库中
////        User user = userMapper.findByEmail(email);
////        System.out.println(user);
////        if (null == user)
////            return -1;
////        // 到这进行匹配
////        // 获取redis中的验证码
////        ValueOperations vop = redisTemplate.opsForValue();
////        String realCode = (String) vop.get(email);
////        // 表示验证码已过期
////        if (null == realCode || realCode.length() == 0)
////            return -2;
////        // 表示验证成功
////        if (realCode.equals(code)) {
////            // 验证成功
////            // 如果是求职者返回5
////            // 如果是招聘者返回10
////            if (user.getUserType().equals("求职者")) {
////                System.out.println("求职者");
////                return 5;
////            } else {
////                System.out.println("招聘者");
////                return 10;
////            }
////        }
////        return 0;
////    }
//
//    @Override
//    public Employer loadEmployerByUserIdr(Integer userId) {
//        return employerMapper.findByUserId(userId);
//    }
//
//    @Override
//    public Employee loadEmployeeByUserIdr(Integer userId) {
//        return employeeMapper.findNameByUserId(userId);
//    }

//    @Override
//    public Integer sendEmailCode(String email) {
//        // 首先判断该邮箱是否存在数据库中
//        User user = userMapper.findByEmail(email);
//        if (null == user)
//            return -1;
//
//        // 这一步表示该邮箱是存在在数据库中的,进行邮件发送功能
//        SimpleMailMessage msg = new SimpleMailMessage();
//        msg.setFrom("2294014200@qq.com");
//        msg.setSubject("轻聘-简单生活,轻松应聘");
//        // 获取验证码
//        int num = (int) (Math.random() * 90000 + 10000);
//        // 把验证码放到redis中并设置过期时间为五分钟
//        ValueOperations vop = redisTemplate.opsForValue();
//        vop.set(email, String.valueOf(num), 5, TimeUnit.MINUTES);
//
//        // 发送验证码
//        msg.setText("验证码:" + num + "\n请在五分钟内完成验证");
//        msg.setTo(email);
//        // 真正的发送
//        mailSender.send(msg);
//        return 1;
//    }
}
